package com.partners.controller.Page;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.partners.annotation.Auths;
import com.partners.annotation.SimpleParam;
import com.partners.constant.PermissionEnum;
import com.partners.dao.dto.UserInfoOutputDTO;
import com.partners.service.FansService;
import com.partners.service.UserService;
import com.partners.utils.CodeUtils;
import com.partners.utils.JsonUtils;
import com.partners.utils.SessionManager;
import com.partners.utils.TimestampUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.parser.HttpParser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author liangyongle
 * @date 2018-12-13
 */
@Controller
@RequestMapping("userPage")
@Slf4j
public class UserPageController {

    @Resource
    private UserService userService;


    @Resource
    private SessionManager sessionManager;

    @Resource
    private FansService fansService;

    /**
     * 用户中心
     * */
    @GetMapping("usercenter")
    @Auths
    public ModelAndView userCenter(HttpSession session){
        Object fansInfo = session.getAttribute("openId");
        //查询用户信息
        JSONObject jsonObject = JSON.parseObject(JsonUtils.toJson(fansInfo));
        Integer uid = jsonObject.getInteger("userId");
        Object fans = fansService.getFansInfoByOpenId(jsonObject.getString("openid"));
        if(fans !=null){
            session.setAttribute("openId",fans);
            JSONObject jsonFans = JSON.parseObject(JsonUtils.toJson(fans));
             uid = jsonFans.getInteger("userId");
        }

        //判断是否有权限进入
        UserInfoOutputDTO userInfo = userService.getUserInfo(uid);
        ModelAndView mav =  new ModelAndView();
        if(userInfo !=null){
            mav.setViewName("show/user_center");
            mav.addObject("uid",uid);
            if(uid ==0){
                String openId = jsonObject.getString("openid");
                Object fan = fansService.getFansInfoByOpenId(openId);
                JSONObject json = JSON.parseObject(JsonUtils.toJson(fans));
                session.setAttribute("openId",json);
                uid = json.getInteger("userId");
            }
        }else{
            mav.setViewName("show/error");
            mav.addObject("code",404);
            mav.addObject("msg","无操作权限");
        }
        return mav;
    }

    /**
    * 添加用户
    * */
    @GetMapping("adduser")
    @Auths
    public ModelAndView addUser(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        ModelAndView mav =  new ModelAndView("show/adduser");
        mav.addObject("uid",uid);
        return mav;
    }


    /**
     * 获取用户的二维码
     *
     * */
    @GetMapping("userCode")
    public ModelAndView showUserCode(@RequestParam(value="id", required=false) Integer id,HttpSession session){
        ModelAndView mav =new ModelAndView("show/user_code");
        mav.addObject("id",id);
        return mav;
    }

    /**
     * 获取我推荐的合伙人
     *
     * */
    @GetMapping("getMyuser")
    @Auths
    public ModelAndView getMyUser(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        ModelAndView mav =new ModelAndView("show/user_list");
        mav.addObject("uid",uid);
        return mav;
    }

    /**
     * 获取合伙人详情
     *
     * */
    @GetMapping("getParter/{id}")
    public ModelAndView getUserDetail(@PathVariable("id") Integer id){
        ModelAndView mav =new ModelAndView("show/parter_detail");
        mav.addObject("uid",id);
        return mav;
    }

    /**
    * 获取报备的列表
    *
    * */
    @GetMapping("getCustomer")
    @Auths
    public ModelAndView getCustomer(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        ModelAndView mav =new ModelAndView("show/select_customer");
        mav.addObject("uid",uid);
        return mav;
    }


    /**
     * 获取员工角色的设置
     *
     * */
    @GetMapping("user_role/{id}")
    @Auths
    public ModelAndView userRole(HttpSession session,@PathVariable("id") Integer id){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        ModelAndView mav = new ModelAndView("show/user_role");
        mav.addObject("uid",uid);
        mav.addObject("id",id);
        return mav;
    }

    /**
    * 员工管理中心
    *
    * */
    @GetMapping("manager")
    @Auths
    public ModelAndView userManager(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        ModelAndView mav = new ModelAndView("show/user_manager");
        mav.addObject("uid",uid);
        return mav;
    }

    /**
    * 管理员-员工管理
    *
    * */
    @GetMapping("group_user/{id}")
    @Auths
    public  ModelAndView userGroup(HttpSession session,@PathVariable("id") Integer id){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        Integer code = PermissionEnum.GROUP_LIST.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/user_group";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        mav.addObject("id",id);
        return mav;
    }

    /**
     * 管理员-员工分组编辑
     * */
    @GetMapping("group_set/{id}")
    public ModelAndView GroupSet(@PathVariable("id") Integer id){
        ModelAndView mav = new ModelAndView("show/group_set");
        mav.addObject("id",id);
        return mav;
    }

    /**
     * 管理员-用户详情
     *
     * */
    @GetMapping("user_detail/{id}")
    public ModelAndView userDetail(@PathVariable("id") Integer id){
        ModelAndView mav = new ModelAndView("show/user_detail");
        mav.addObject("uid",id);
        return mav;
    }


    /**
     * 管理员-编辑角色和权限
     *
     * */
    @GetMapping("edit_role/{pid}")
    //@Auths
    public  ModelAndView roleEdit(@PathVariable("pid") Integer pid,@RequestParam(value="id", required=false) Integer id){
        id = id != null?id:0;
//        JSONObject jsonObject = sessionManager.JsonSession(session);
//        Integer uid = jsonObject.getInteger("userId");
        ModelAndView mav = new ModelAndView("show/role_editUserRoleOutputDTO");
        //mav.addObject("uid",uid);
        mav.addObject("id",id);
        mav.addObject("pid",pid);
        return mav;
    }


    /**
     * 用户-地址列表
     *
     * */
    @GetMapping("address_list")
    @Auths
    public ModelAndView addressSet(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        ModelAndView mav = new ModelAndView("show/address_list");
        mav.addObject("uid",uid);
        return mav;
    }


    /**
     * 用户-地址详情
     *
     * */
    @GetMapping("address_detail")
    @Auths
    public ModelAndView addressDetail(HttpSession session, @RequestParam(value="id", required=false) Integer id){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        id = id !=null?id:0;
        Integer code = PermissionEnum.ADDRESS_EDIT.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/address_detail";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        mav.addObject("id",id);
        return mav;
    }

    /**
     * 分组列表
     * @return
     */

    @GetMapping("group_list")
    @Auths
    public ModelAndView getGroupList(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        Integer code = PermissionEnum.GROUP_LIST.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/group_list";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        return mav;
    }


    /**
     * 合伙人列表
     * @return
     */
    @GetMapping("parner_list")
    @Auths
    public ModelAndView getParnerList(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        Integer code = PermissionEnum.MY_USER.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/parner_list";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        Integer timestamp = TimestampUtils.timestamp();
        mav.addObject("uid",uid);
        mav.addObject("timestamp",timestamp);
        return mav;
    }



    /**
     * 分组编辑
     * @return
     */
    @GetMapping("group_edit")
    @Auths
    public ModelAndView editGroup(HttpSession session,@RequestParam(value="id", required=false) Integer id){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        id = id !=null?id:0;
        Integer code = PermissionEnum.GROUP_EDIT.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/group_edit";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        mav.addObject("id",id);
        return mav;
    }


    /**
     * 获取角色下面的用户
     * @return
     */
    @GetMapping("role_user/{id}")
    @Auths
    public ModelAndView roleUser(HttpSession session,@PathVariable("id")  Integer id){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        id = id !=null?id:0;
        Integer code = PermissionEnum.GROUP_EDIT.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/role_user";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        mav.addObject("id",id);
        return mav;
    }

    /**
     * 编辑用户信息
     * @return
     * */
    @GetMapping("edit/{id}")
    @Auths
    public ModelAndView userEdit(HttpSession session,@PathVariable("id")  Integer id){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        id = id !=null?id:0;
        Integer code = PermissionEnum.EDIT_USER.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/user_edit";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        mav.addObject("id",id);
        return mav;
    }




    @GetMapping("test")
    public ModelAndView addressSet(){
        ModelAndView mav = new ModelAndView("show/test");
        return mav;
    }


}
